package com.yc.thread;

/**
 * @program: concurrent
 * @description: yield让位，屈服
 * @author: zy
 * @create: 2022-01-10 12:10
 */
public class Test12_yield {
    // 挂起线程
    public static void main(String[] args) {
        YieldOne y1 = new YieldOne();
        YieldOne y2 = new YieldOne();

        Thread t1 = new Thread(y1, "a");
        Thread t2 = new Thread(y2, "b");

        t1.setPriority(1);
        t1.start();

        t2.setPriority(10);    //第二个线程的you先级高
        t2.start();


    }

}

class YieldOne implements Runnable {
    @Override
    public void run() {
        if ("a".equals(Thread.currentThread().getName())) {
            Thread.yield();              //TODO:yield只会将执行权放给you先级高的线程
//            try {
//
//                Thread.sleep(100);         //TODO: sleep不管you先级，只要调用sleep，则当前线程睡，其它接过执行权
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + i);
        }
    }

}